package com.yoomiito.app.ui.my.teamallowance

import android.net.Uri
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.my.MyOrderBean
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.widget.CircleImageView

class TeamAllowanceOrderAdapter(data:List<MyOrderBean>?,var type:Int) :BaseQuickAdapter<MyOrderBean,BaseViewHolder>(R.layout.item_my_order,data){

    override fun convert(helper: BaseViewHolder?, item: MyOrderBean?) {
        helper?.setText(R.id.tv_name, item?.nickname)
        if (TextUtils.isEmpty(item?.order_source)) {
            (helper?.getView<View>(R.id.tv_order_num) as TextView).text = ResUtil.getString(R.string.my_order_num) + item?.sn
        } else {
            (helper?.getView<View>(R.id.tv_order_num) as TextView).text = ResUtil.getString(R.string.my_order_num) + item?.order_id
        }
        if (Constant.NAMAL_TYPE_TAOBAO != type) {
            val goodsName = helper.getView<TextView>(R.id.item_order_goods_name)
            goodsName.text = item?.product_name
            goodsName.visibility = View.VISIBLE
        }

        (helper.getView<View>(R.id.tv_money) as TextView).text = "" + SpannableUtils.handlePrice(item?.getUser_income())
        val requestOptions = RequestOptions()
        requestOptions.placeholder(R.drawable.goods_default)
        if (!TextUtils.isEmpty(item?.headimgurl)) {
            Glide.with(App.getApplication()).load(Uri.parse(item?.headimgurl)).apply(requestOptions).into(helper.getView<View>(R.id.iv_photo) as CircleImageView)
        }
        val flagIv = helper.getView<ImageView>(R.id.iv_st)
        if (Constant.NAMAL_TYPE_TAOBAO == type) {

            flagIv.visibility = View.VISIBLE
            //            if ("天猫".equals(item.getOrder_source())){
            //                tmIconIv.setImageResource(R.mipmap.tmall);
            //            }else {
            //                tmIconIv.setImageResource(R.mipmap.tb);
            //            }
            if ("淘宝" == item?.order_source) {
                flagIv.setImageResource(R.mipmap.tb)
            } else if ("天猫" == item?.order_source) {
                flagIv.setImageResource(R.mipmap.tmall)
            } else if ("聚划算" == item?.order_source) {
                flagIv.setImageResource(R.mipmap.juhuasuan)
            } else if ("航旅" == item?.order_source) {
                flagIv.setImageResource(R.mipmap.feizhu)
            } else if ("天猫国际" == item?.order_source) {
                flagIv.setImageResource(R.mipmap.tmall_guoji)
            } else {
                flagIv.visibility = View.GONE
            }
        } else {
            if (TextUtils.isEmpty(item?.image_url)) {
                flagIv.visibility = View.GONE
            } else {
                flagIv.visibility = View.VISIBLE
                ImageLoader.getInstance().into(flagIv.context, item?.image_url, flagIv)
            }
        }
        val failView = helper.getView<View>(R.id.item_my_order_fail)
        when (item?.status) {
            Constant.ORDER_STATUS_WAIT_PAY -> {
                failView.visibility = View.GONE
                helper.getView<View>(R.id.iv_status).visibility = View.GONE
            }
            Constant.ORDER_STATUS_PAYED -> {
                failView.visibility = View.VISIBLE
                helper.getView<View>(R.id.iv_status).visibility = View.VISIBLE
                (helper.getView<View>(R.id.iv_status) as ImageView).setImageResource(R.mipmap.already_settled)
            }
            Constant.ORDER_STATUS_CANCEL_PAY -> {
                failView.visibility = View.VISIBLE
                helper.getView<View>(R.id.iv_status).visibility = View.VISIBLE
                (helper.getView<View>(R.id.iv_status) as ImageView).setImageResource(R.mipmap.failure)
            }
        }
        helper?.setText(R.id.tv_date, item?.ctime)

    }

}